﻿using System;
using System.Linq;
using System.Web.Mvc;
using DNB.Data;
using DNB.Models;

namespace DNB.Controllers.Map
{
    public class MapTopController : Controller
    {
        public ActionResult Index()
        {
            var db = new DemoDBEntities();
            var model = db.LOCATIONs.Select(m => new MapTopModels()
                {
                    Lon = m.X,
                    Lat = m.Y,
                    Name = m.Name
                }).ToList();

            return View(model);
        }

        public ActionResult MapTopTwo()
        {
            var db = new DemoDBEntities();
            var model = db.LOCATIONs.Select(m => new MapTopModels()
                {
                    Lon = m.X,
                    Lat = m.Y,
                    Name = m.Name
                }).ToList();

            return View(model);
        }

        public ActionResult FusionTableLayer()
        {
            return View();
        }

        public ActionResult FusionTableLayerTwo()
        {
            return View();
        }

        public ActionResult KmlGeoRss()
        {
            return View();
        }

        public ActionResult InsertLatLgPage(FormCollection collection)
        {
            DemoDBEntities context = new DemoDBEntities();
            if (Request.HttpMethod == "POST")
            {
                var maxId = (from s in context.LOCATIONs select s.Id).Max();

                LOCATION mapTop = new LOCATION
                    {
                        Id = maxId + 1,
                        Name = collection.Get("Name"),
                        X = Convert.ToDecimal(collection.Get("Latitude")),
                        Y = Convert.ToDecimal(collection.Get("Longitude"))
                    };
                context.AddToLOCATIONs(mapTop);
                context.SaveChanges();
                return RedirectToAction("ViewAndInterationPage", "MapTop");
            }

            return View();
        }

        public ActionResult ViewAndInterationPage()
        {
            DemoDBEntities context = new DemoDBEntities();
            var location = from s in context.LOCATIONs select s;
            return View(location.ToList());
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EditLatLgPage(string id)
        {
            DemoDBEntities context = new DemoDBEntities();
            int idLocation = Convert.ToInt32(id);
            LOCATION location = context.LOCATIONs.Single(n => n.Id == idLocation);
            return View(location);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditLatLgPage(LOCATION locations)
        {
            //Get location specifix from databaser
            using (DemoDBEntities context = new DemoDBEntities())
            {
                var locaion = (from s in context.LOCATIONs where s.Id == 0 select s).First();
                locaion.Name = locations.Name;
                locaion.X = locations.X;
                locaion.Y = locations.Y;
                context.SaveChanges();
            }
            return RedirectToAction("ViewAndInterationPage");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult DeleteLatLgPage(LOCATION locations)
        {
            //Get location specifix from databaser
            using (DemoDBEntities context = new DemoDBEntities())
            {
                var locaion = (from s in context.LOCATIONs where s.Id == locations.Id select s).First();

                context.DeleteObject(locaion);
                context.SaveChanges();
            }
            return RedirectToAction("ViewAndInterationPage");
        }
    }
}